#!/bin/sh

driver_ver=linux-touch-driver

os_name="NONE"
os_ver="NONE"
type=r

spec_os=NONE

get_centos_version()
{
	for VER in 5 6 7 8 9
	do
		xver=$(uname -r 2>/dev/null | grep -ic el$VER)

		if [ "$xver" != "0" ]; then
			echo $VER
			return
		fi
	done

	echo "NONE"
}

get_fedora_version()
{
	for VER in 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
	do
		xver=$(uname -r 2>/dev/null | grep -ic fc$VER)

		if [ "$xver" != "0" ]; then
			echo $VER
			return
		fi
	done

	echo "NONE"
}

get_suse_version()
{
	for VER in 11 12 15
	do
		xver=$(cat /etc/os-release | grep "PRETTY_NAME" | grep -ic $VER)
		if [ "$xver" != "0" ]; then
			echo $VER
			return
		fi
	done

	echo "NONE"
}

get_debian_version()
{
	osver="NONE"
	
	for debian_name in bullseye buster stretch jessie wheezy ueeze
	do
		ret=$(lsb_release -da 2>/dev/null | grep -ic "$debian_name")
			
		if [ "$ret" != "0" ]; then
			case $debian_name in
				bullseye)osver=11;;
				buster)osver=10;;
				stretch)osver=9;;
				jessie)osver=8;;
				wheezy)osver=7;;
				ueeze)osver=6;;
			esac
			
			echo $osver
			return
		fi		
	done
}

get_slackware_version()
{
	osver="NONE"
	
	if [ -f /etc/os-release ]; then
		osver=$(cat /etc/os-release | grep "VERSION_ID" | grep -o "[0-9].*")	
	elif [ -f /etc/slackware-version ]; then
		osver=$(cat /etc/slackware-version | grep "Slackware" | grep -o "[0-9].*")
	fi
	
	echo $osver
}

get_opensuse_version()
{
	osver="NONE"
	
	if [ -f /etc/os-release ]; then
		osver=$(cat /etc/os-release | grep 'PRETTY_NAME' | grep -o -E "[0-9]+\.[0-9]")
	fi

	if [ "$osver" = "" ] || [ "$osver" = "NONE" ]; then
		if [ -f /usr/bin/lsb-release ]; then
			osver=$(/usr/bin/lsb-release -a 2>/dev/null | grep 'Description' | grep -o -E "[0-9]+\.[0-9]")
		fi
	fi
    
	if [ "$osver" = "" ] || [ "$osver" = "NONE" ]; then
		if [ -f /usr/bin/lsb_release ]; then
			osver=$(/usr/bin/lsb_release -a 2>/dev/null | grep 'Description' | grep -o -E "[0-9]+\.[0-9]")
		fi
	fi
	
	if [ "$osver" = "" ] || [ "$osver" = "NONE" ]; then
		if [ -f /usr/bin/lsb_release ]; then
			osver=$(/usr/bin/lsb_release -a 2>/dev/null | grep 'Release' | grep -o -E "[0-9]+\.[0-9]")
		fi
	fi
	
	if [ "$osver" = "" ] || [ "$osver" = "NONE" ]; then
		if [ -f /usr/bin/lsb_release ]; then
			osver=$(/usr/bin/lsb_release -a 2>/dev/null | grep 'Release' | grep -o -E "[0-9]+\.[0-9]")
		fi
	fi
	
	echo $osver
}

get_ubuntu_version()
{
	osver=$(lsb_release -a 2>/dev/null | grep "Release" | grep -o "[0-9].*")
	echo $osver
}

get_uos_version()
{
	osver=$(lsb_release -a 2>/dev/null | grep "Release" | grep -o "[0-9].*")
	echo $osver
}

get_mint_version()
{
	osver=$(cat /etc/lsb-release | grep "DISTRIB_RELEASE" | grep -o "[0-9].*")
	echo $osver
}

get_neokylin_version()
{
	osver=$(cat /etc/os-release | grep "VERSION_ID" | grep -o "[0-9].[0-9]")
	echo $osver
}

set_xinput_ver(){
	_os_name=$1
	_os_ver=$2
	
	xver="NONE"
	
	if [ "$_os_name" = "fedora" ]; then
		ret=$(./checkver bc "$_os_ver" "25")
		if [ "$ret" = "1" ] || [ "$ret" = "0" ]; then
			xver="24.1"
		else
			case $_os_ver in
				23)xver="22.1";;	
				24)xver="22.1";;
			esac
		fi
	elif [ "$_os_name" = "ubuntu" ]; then
		ret=$(./checkver bc "$_os_ver"  17.04)

		if [ "$ret" = "1" ] || [ "$ret" = "0" ]; then
			xver="24.1"
		fi
	elif [ "$_os_name" = "debian" ]; then
		ret=$(./checkver bc "$_os_ver"  "10")

		if [ "$ret" = "1" ] || [ "$ret" = "0" ]; then
			xver="24.1"
		fi
	elif [ "$_os_name" = "centos" ] || [ "$_os_name" = "rhel" ]; then
		case $_os_ver in
			7)xver="24.1";;
			8)xver="24.1";;
		esac
	elif [ "$_os_name" = "suse" ]; then
		ret=$(./checkver bc "$_os_ver"  "15")

		if [ "$ret" = "1" ] || [ "$ret" = "0" ]; then
			xver="24.1"
		fi
	elif [ "$_os_name" = "opensuse" ]; then
		ret=$(./checkver bc "$_os_ver"  "15.0")

		if [ "$ret" = "1" ] || [ "$ret" = "0" ]; then
			xver="24.1"
		fi
	elif [ "$_os_name" = "slackware" ]; then
		case $_os_ver in
			"12.1")xver="2.0";;
			"12.0")xver="0.7";;
			"13.0")xver="4.0";;
		esac
	fi
	
	echo $xver
}

get_xinput_version_by_xorg_ver()
{
	for xx in 4 6 7 9 10 11 12 13 14 15 16 17 18 19 20
	do
		ret=$(echo $1 | grep -ic "1.$xx")

		if [ "$ret" != "0" ]; then
			case $xx in
				4) version=4.0 ;;
				6) version=7.0 ;;
				7) version=9.0 ;;
				9) version=1.0 ;;
				10) version=12.2 ;;
				11) version=13.0 ;;
				12) version=16.0 ;;
				13) version=18.0 ;;
				14) version=19.1 ;;
				15) version=20.0 ;;
				16) version=21.0 ;;
				17) version=21.0 ;;
				18) version=22.1 ;;
				19) version=24.1 ;;
				20) version=24.1 ;;
			esac
			
			echo "$version"
			return
		fi
	done
	
	echo "NONE"
}

find_bin()
{
	name=$1
	
	for bin_path in /bin /sbin /usr/bin /usr/local/bin
	do
		if  [ -f $bin_path/$name ]; then
			echo "1"
			return
		fi
	done
	
	echo "0"
}

get_xinput_version()
{
	version="NONE"

	if [ -f ./Xorg.0.log ]; then
		rm ./Xorg.0.log
	fi

	if [ -f /var/log/Xorg.0.log ]; then
		cp -f /var/log/Xorg.0.log ./
	else
		if [ -f ~/.local/share/xorg/Xorg.0.log ]; then
			cp -f ~/.local/share/xorg/Xorg.0.log ./
		fi
		
		if [ -f ~/.local/share/xorg/Xorg.1.log ]; then
			cp -f ~/.local/share/xorg/Xorg.1.log ./Xorg.0.log
		fi		
	fi
	
	if [ "$version" = "NONE" ]; then	
		if [ -f ./Xorg.0.log ]; then
			version=$(./checkver x)
		fi
	fi	
	
	if [ "$version" = "NONE" ]; then	
		ret=$(find_bin xdpyinfo) 
	
		if [ "$ret" = "1" ]; then
			xorg_ver=$(xdpyinfo 2>/dev/null | grep 'X.Org version')
			version=$(get_xinput_version_by_xorg_ver "$xorg_ver")
		fi
	fi
	
	if [ "$version" = "NONE" ]; then
		xorg_ver=$(./checkver xorg)

		if [ "$xorg_ver" != "NONE" ]; then
			version=$(get_xinput_version_by_xorg_ver "$xorg_ver")
			break
		fi
	fi
		
	if [ "$version" = "NONE" ]; then
		version=$(set_xinput_ver $2)
	fi

	if [ "$version" = "NONE" ]; then
		ret=$(find_bin journalctl) 
	
		if [ "$ret" = "1" ]; then
			xver=$(journalctl | grep "X.Org XInput driver" | grep -ic 'X.Org XInput driver : 21.0')
			if [  "$xver" != "0"  ]; then
				version=21.0
			else
				xver=$(journalctl | grep "X.Org XInput driver" | grep -ic 'X.Org XInput driver : 24.1')
				if [  "$xver" != "0"  ]; then
					version=24.1
				fi
			fi
		fi
	fi

	if [ "$version" = "18.1" ]; then
		version=18.0
	elif [ "$version" = "19.2" ]; then
		version=19.1
	elif [  "$version" = "12.3"  ]; then
		version=12.2
	fi
	
	echo $version
}

get_os_name()
{
	_os_name="NONE"
	
	if [ -f /etc/lsb-release ]; then
		ret=$(cat /etc/lsb-release | grep -ic ubuntu)
		if [ "$ret" != "0" ]; then
			_os_name=ubuntu
		fi
		
		if [ "$_os_name" = "NONE" ]; then
			ret=$(cat /etc/lsb-release | grep -ic Mint)
			if [ "$ret" != "0" ]; then
				_os_name=mint
			fi
		fi				
	fi

	if [ "$_os_name" = "NONE" ] && [ -f /etc/system-release ]; then
		ret=$(cat /etc/system-release | grep -ic Scientific)
		
		if [ "$ret" != "0" ]; then
			_os_name=scientific
		fi	
	fi
	
	if [ "$_os_name" = "NONE" ] && [ -f /etc/centos-release ]; then
		#cat /etc/centos-release
		_os_name=centos
	fi

	if [ "$_os_name" = "NONE" ] && [ -f /etc/fedora-release ]; then
		#cat /etc/fedora-release
		_os_name=fedora
	fi

	if [ "$_os_name" = "NONE" ] && [ -f /etc/redhat-release ]; then
		#cat /etc/redhat-release
		
		ret=$(cat /etc/redhat-release | grep -ic Red)
		if [ "$ret" != "0" ]; then
			_os_name=rhel
		fi
		
		if [ "$_os_name" = "NONE" ]; then
			ret=$(cat /etc/redhat-release | grep -ic CentOS)
			if [ "$ret" != "0" ]; then
				_os_name=centos
			fi
		fi
		
		if [ "$_os_name" = "NONE" ]; then
			ret=$(cat /etc/redhat-release | grep -ic Fedora)
			if [ "$ret" != "0" ]; then
				_os_name=fedora
			fi
		fi
	fi

	if [ "$_os_name" = "NONE" ] && [ -f /etc/slackware-version ]; then
		ret=$(cat /etc/slackware-version | grep -ic Slackware)
		if [ "$ret" != "0" ]; then
			_os_name=slackware
		fi
	fi

	if [ "$_os_name" = "NONE" ] && [ -f /etc/os-release ]; then
		ret=$(cat /etc/os-release | grep -ic NeoKylin)
		if [ "$ret" != "0" ]; then
			_os_name=neokylin
		fi
		
		if [ "$_os_name" = "NONE" ]; then
			ret=$(cat /etc/os-release | grep -ic openSUSE)
			
			if [ "$ret" != "0" ]; then
				_os_name="opensuse"
			fi
		fi
			
		if [ "$_os_name" = "NONE" ]; then
			ret=$(cat /etc/os-release | grep -ic Slackware)
			
			if [ "$ret" != "0" ]; then
				_os_name="slackware"
			fi	
		fi

		if [ "$_os_name" = "NONE" ]; then
			ret1=$(cat /etc/os-release | grep -ic SLE)
			ret2=$(cat /etc/os-release | grep -ic SUSE)
			
			if [ "$ret1" != "0" ] && [ "$ret2" != "0" ]; then
				_os_name="suse"
			fi
		fi
		
		#cat /etc/os-release | grep 'PRETTY_NAME'
	fi

	if [ "$_os_name" = "NONE" ] && [ -f /etc/issue.net ]; then
		ret=$(cat /etc/issue.net | grep -ic "SUSE Linux Enterprise Server 11")
		if [ "$ret" != "0" ]; then
			_os_name="suse"
			#cat /etc/issue.net
		fi	
	fi

	if [ "$_os_name" = "NONE" ] && [ -f /usr/bin/lsb-release ]; then
		ret=$(/usr/bin/lsb-release -a 2>/dev/null | grep -ic openSUSE)
		
		if [ "$ret" != "0" ]; then
			_os_name="opensuse"			
		fi
	fi

	if [ "$_os_name" = "NONE" ] && [ -f /usr/bin/lsb_release ]; then
		ret=$(/usr/bin/lsb_release -a 2>/dev/null | grep -ic openSUSE)
		
		if [ "$ret" != "0" ]; then
			_os_name="opensuse"			
		fi

		if [ "$_os_name" = "NONE" ]; then
			ret=$(lsb_release -da 2>/dev/null | grep -ic debian)

			if [ "$ret" != "0" ]; then
				_os_name=debian
			fi
		fi
		
		if [ "$_os_name" = "NONE" ]; then
			ret=$(lsb_release -da 2>/dev/null | grep -ic Raspbian)

			if [ "$ret" != "0" ]; then
				_os_name=debian
			fi
		fi
				
		if [ "$_os_name" = "NONE" ]; then
			ret=$(lsb_release -da 2>/dev/null | grep -ic uos)

			if [ "$ret" != "0" ]; then
				_os_name=uos
			fi
		fi
		
		if [ "$_os_name" = "NONE" ]; then
			ret=$(lsb_release -da 2>/dev/null | grep -ic 'Kylin V10')

			if [ "$ret" != "0" ]; then
				_os_name=kylin
			fi
		fi		
	fi
	
	echo $_os_name
}

get_gtk_vet()
{
	for LIB_DIR in /usr/lib64 /usr/lib/x86_64-linux-gnu /usr/lib /usr/lib/i386-linux-gnu /lib/x86_64-linux-gnu /lib/i386-linux-gnu /lib /lib64 /usr/lib/aarch64-linux-gnu/ /usr/lib/arm-linux-gnueabihf
	do
		if [ -d $LIB_DIR ]; then
			ret=$(ls $LIB_DIR 2>/dev/null | grep -ic libgtk-3)

			if [ "$ret" != "0" ]; then
				echo "3"
				return
			else
				ret1=$(ls $LIB_DIR 2>/dev/null | grep -ic libgtk-2)
				ret2=$(ls $LIB_DIR 2>/dev/null | grep -ic libgtk-x11-2)
				
				if [ "$ret1" != "0" ] || [ "$ret2" != "0" ]; then
					echo "2"
					return
				fi
			fi
		fi
	done
	
	echo "0"
}

get_libusb_ver()
{
	for C_LIB_DIR in /usr/lib64 /usr/lib/x86_64-linux-gnu /usr/lib /usr/lib/i386-linux-gnu /lib/x86_64-linux-gnu /lib/i386-linux-gnu /lib /lib64 /usr/lib/aarch64-linux-gnu/ /usr/lib/arm-linux-gnueabihf
	do
		if [ -d $C_LIB_DIR ]; then
			for ver in "1.0" "0.1"
			do
				ret=$(ls $C_LIB_DIR 2>/dev/null | grep -ic libusb-$ver)
				if [ "$ret" != "0" ]; then
					if [ $ver = "1.0" ]; then
						echo 1
					else
						echo 0
					fi
					return
				fi
			done
		fi
	done
	
	echo "NONE"
}

get_libpng_ver()
{
	for C_LIB_DIR in /usr/lib64 /usr/lib/x86_64-linux-gnu /usr/lib /usr/lib/i386-linux-gnu /lib/x86_64-linux-gnu /lib/i386-linux-gnu /lib /lib64 /usr/lib/aarch64-linux-gnu/ /usr/lib/arm-linux-gnueabihf
	do
		if [ -d $C_LIB_DIR ]; then
			for ver in 16 15 12
			do
				ret=$(ls $C_LIB_DIR 2>/dev/null | grep -ic libpng$ver)
				
				if [ "$ret" != "0" ]; then
					echo $ver
					return
				fi
			done
		fi
	done
	echo "NONE"
}

get_osver()
{
	osver="NONE"
	
	case $1 in
		ubuntu) osver=$(get_ubuntu_version);;
		fedora) osver=$(get_fedora_version);;
		rhel) osver=$(get_centos_version);;		
		centos) osver=$(get_centos_version);;
		slackware) osver=$(get_slackware_version);;
		neokylin) osver=$(get_neokylin_version);;
		opensuse) osver=$(get_opensuse_version);;
		debian) osver=$(get_debian_version);;
		suse) osver=$(get_suse_version);;
		uos) osver=$(get_uos_version);;
		mint) osver=$(get_mint_version);;
		scientific) osver=7.9;;
		kylin) osver=10;;
		* ) osver="NONE";;
	esac
	
	if [ "$osver" = "" ]; then
		osver="NONE"
	fi
	
	echo $osver
}

os_name=$(get_os_name)

if [ "$spec_os" != "NONE" ]; then
	if [ "$os_name" != $spec_os ]; then
		os_name="NONE"
	fi
fi

if [ "$os_name" = "" ] || [ "$os_name" = "NONE" ]; then
	echo "This OS is not supported!!"
	exit 0
fi

os_ver=$(get_osver $os_name)

if [ "$os_ver" = "" ] || [ "$os_ver" = "NONE" ]; then
	echo "This OS version is not supported!!"
	exit 0
fi

if [ "$os_name" = "mint" ] && [ "$os_ver" != "20" ]; then
	echo "This OS version is not supported!!"
	exit 0	
fi

if [ "$os_name" = "ubuntu" ]; then
	ver=$(lsb_release -a 2>/dev/null | grep "Description:" | grep -o "[0-9].*")
else
	ver=$os_ver
fi

machine="NONE"

ret=$(file /bin/bash 2>/dev/null | grep -ic 'ARM')

if [ "$ret" = "1" ]; then
	ret=$(file /bin/bash 2>/dev/null | grep -ic 'aarch64')
	
	if [ "$ret" = "1" ]; then
		machine=aarch64
	else
		ret=$(file /bin/bash 2>/dev/null | grep -ic 'armhf')
		
		if [ "$ret" = "1" ]; then
			machine=armhf
		fi
	fi
else
	ret=$(file /bin/bash 2>/dev/null | grep -ic '32-bit')

	if [ "$ret" = "1" ]; then
		machine=i686
	else
		machine=x86_64
	fi
fi

if [ "$machine" = "NONE" ]; then
	file /bin/bash
	echo "This machine is not supported!!"
fi

if [ "$os_name" = "centos" ]; then
	if [ -f /etc/centos-release ]; then
		cat /etc/centos-release
		echo $machine
	else
		echo $os_name $ver $machine
	fi
else
	echo $os_name $ver $machine
fi

#echo $os_name $os_ver
DRV_DIR=/usr/lib/xorg/modules/input/
DRV_DIR64=/usr/lib64/xorg/modules/input/

if [ -f $DRV_DIR/risintech_drv.so ] || [ -f $DRV_DIR64/risintech_drv.so ] || [ -f $DRV_DIR/xfhiddrv_drv.so ] || [ -f $DRV_DIR64/xfhiddrv_drv.so ]; then
	if [ -f /opt/.touch_panel/.config ]; then
		if [ -f /opt/.touch_panel/uin ] || [ -f /opt/.touch_panel/nuin ]; then
			sh remove.sh
		else
			echo "You Have already installed a driver,"
			echo "If you want to install the new driver,"
			echo "Please uninstall the old one first."
			exit 0
		fi
	else
		echo "You Have already installed a driver,"
		echo "If you want to install the new driver,"
		echo "Please uninstall the old one first."
		exit 0
	fi
fi

#echo $machine

# Decompress tar
if [ -d ./$driver_ver ]; then
	rm -rf ./$driver_ver
fi

tar xmzf $driver_ver.tgz > tar.log
cd $driver_ver

if [ ! -d "$machine" ]; then
	echo "This machine is not supported. [$machine]"
	exit 1;
fi

cd $machine

if [ ! -f ./checkver ]; then
	echo "This X Server version is not supported. [app 0]"
	exit 1
fi

chmod 777 checkver

if [ "$os_name" = "slackware" ]; then
	os_ver=$(./checkver ver "$os_ver")
	
	if [ "$os_ver" = "12.2" ]; then
		echo "This OS version is not supported!!"
		exit 0
	fi
fi

if [ -f /opt/.touch_panel/.config ]; then	
	if [ -f ./closedev ]; then
		chmod 777 closedev
		./closedev
	fi
	
	cd ..
	sh clearlib.sh
	cd $machine
fi

ldd_str=$(ldd --version 2>/dev/null | head -n1)
curr_ldd=$(./checkver ver "$ldd_str")

echo system ldd:$curr_ldd

if [ "$curr_ldd" = "NONE" ]; then
  echo "This X Server version is not supported. [app 1]"
  exit 1
fi

xinput_version=$(get_xinput_version $os_name $os_ver)

echo "X.Org XInput driver :"$xinput_version

if [ "$xinput_version" = "NONE" ]; then
	echo "This X Server version is not supported. [none]"
	exit 1
fi

uin=1

if [ $uin = "2" ]; then
	echo "0.xinput"
	echo "1.uinput"
	read -p "Select:" drv_mode

	if [ "$drv_mode" = "1" ]; then
		uin=1
	else
		uin=0
	fi
else
	#xinput_version 0.6~13.0
	ret=$(./checkver bc "$xinput_version" "13.0")
	if [ "$ret" = "2" ] || [ "$ret" = "0" ]; then
		uin=0
	fi
	
	kernel=$(uname -r 2>/dev/null)
	ret=$(./checkver ver "$kernel" | grep -ic 2.6)
	if [ "$ret" = "1" ]; then
		uin=0
	fi
fi

wayland_enable=0

if [ "$uin" = "1" ]; then
	if [ "$os_name" != "kylin" ] && [ "$os_name" != "neokylin" ] && [ "$os_name" != "opensuse" ]; then
		wayland_enable=$(./checkver wayland)
		echo wayland_enable:$wayland_enable
	fi
fi

app_ldd_ver="NONE"
driver_ldd_ver="NONE"

driver_ldd_ver=$(./checkver driver "$curr_ldd" "$xinput_version" "./driver")
echo driver version:$driver_ldd_ver

if [ "$driver_ldd_ver" = "" ] || [ "$driver_ldd_ver" = "NONE" ]; then
	echo "This X Server version is not supported. [driver 0]"
	exit 1
fi

app_ldd_ver=$(./checkver app$uin "$curr_ldd")

if [ "$app_ldd_ver" = "" ] || [ "$app_ldd_ver" = "NONE" ]; then
	echo "This X Server version is not supported. [app 2]"
	exit 1
fi

libusb_ver=$(get_libusb_ver)

if [ "$libusb_ver" = "NONE" ]; then
	echo "This X Server version is not supported. [app 3]"
	exit 1
fi

libpng_ver=$(get_libpng_ver)

if [ "$libpng_ver" = "NONE" ]; then
	echo "This X Server version is not supported. [app 4]"
	exit 1
fi

png_ldd_ver="NONE"
gtk_ver=2

if [ "$uin" = "1" ]; then
	gtk_ver=$(get_gtk_vet)
fi

png_ldd_ver=$(./checkver "gtk$uin$gtk_ver" "$curr_ldd" "$libpng_ver")

if [ "$png_ldd_ver" = "" ] || [ "$png_ldd_ver" = "NONE" ]; then
	if [ "$gtk_ver" = "3" ]; then
		gtk_ver=2
		png_ldd_ver=$(./checkver "gtk$uin$gtk_ver" "$curr_ldd" "$libpng_ver")
	fi
fi

if [ "$png_ldd_ver" = "" ] || [ "$png_ldd_ver" = "NONE" ]; then
	echo "This X Server version is not supported. [app 5]"
	exit 1
fi

udev_ver="none"

if [ -f /usr/bin/udevinfo ]; then
	udev_ver=$(udevinfo -V 2>/dev/null | grep -o "[0-9].*")
else
	udev_ver=$(udevadm --v 2>/dev/null)
fi
	
#echo udev_ver:$udev_ver

ret=$(./checkver bc "$udev_ver" "200")
ret=$?

if [ "$ret" = "2" ]; then
	ret=$(./checkver bc "$app_ldd_ver" "2.17")

	if [ "$ret" = "1" ] || [ "$ret" = "0" ]; then
		app_ldd_ver=2.15
	fi
	
	ret=$(./checkver bc "$png_ldd_ver" "2.17")

	if [ "$ret" = "1" ] || [ "$ret" = "0" ]; then
		png_ldd_ver=2.15
	fi	
fi

#echo app version:$app_ldd_ver uin:$uin

if [ $uin = "1" ]; then
	driver_path=./driver/$driver_ldd_ver/$xinput_version/uin
	installed=0
	app_path=./app/$app_ldd_ver/uin
	tools_path=./app/$png_ldd_ver/uin
	
	echo tool ver:$png_ldd_ver[$gtk_ver.$libusb_ver.$libpng_ver]
	
	if [ "$wayland_enable" = "1" ]; then
		if [ -f $tools_path/Touchpack-$gtk_ver-$libpng_ver ]; then		
			echo "***for wayland***"
			cp -f $app_path/setpdrv ./setpdrv
			cp -f $app_path/setdev ./setdev
			cp -f $app_path/tpservice ./tpservice	
			cp -f $tools_path/LinearAp-$gtk_ver-$libpng_ver ./LinearAp
			cp -f $tools_path/FreeDraw-$gtk_ver-$libpng_ver ./FreeDraw
			cp -f $tools_path/Touch_Alignment-$gtk_ver-$libpng_ver ./Touch_Alignment
			cp -f $tools_path/Touchpack-$gtk_ver-$libpng_ver ./Touchpack
			installed=1
		fi
	fi
	
	if [ "$installed" = "0" ]; then
		echo "***for xserver***"
		if [ -f $tools_path/Touchpack-$gtk_ver-$libusb_ver-$libpng_ver ]; then
			cp -f $app_path/setpdrv-$libusb_ver ./setpdrv
			cp -f $app_path/setdev-$libusb_ver ./setdev
			cp -f $app_path/tpservice-$libusb_ver ./tpservice
			cp -f $tools_path/LinearAp-$gtk_ver-$libusb_ver-$libpng_ver ./LinearAp
			cp -f $tools_path/FreeDraw-$gtk_ver-$libusb_ver-$libpng_ver ./FreeDraw
			cp -f $tools_path/Touch_Alignment-$gtk_ver-$libusb_ver-$libpng_ver ./Touch_Alignment	
			cp -f $tools_path/Touchpack-$gtk_ver-$libusb_ver-$libpng_ver ./Touchpack
			installed=1
		fi			
	fi
	
	if [ "$installed" = "0" ]; then
		echo "This X Server version is not supported. [gtk 2]"
	fi
else
	echo "***for xserver***"
	driver_path=./driver/$driver_ldd_ver/$xinput_version
	
	app_path=./app/$app_ldd_ver
	tools_path=./app/$png_ldd_ver
	
	if [ "$os_name" = "slackware" ]; then
		if [ "$xinput_version" = "0.7" ] || [ "$xinput_version" = "2.0" ] || [ "$xinput_version" = "4.0" ]; then
			app_path=./app/$app_ldd_ver/$os_name
			tools_path=./app/$png_ldd_ver/$os_name
		fi
	fi
	
	echo tool ver:$png_ldd_ver[$gtk_ver.$libusb_ver.$libpng_ver]
	
	if [ ! -f $tools_path/Touchpack-$gtk_ver-$libusb_ver-$libpng_ver ]; then
		echo "This X Server version is not supported. [gtk 2]"
		exit 1
	fi	

	cp -f $app_path/setdev-$libusb_ver ./setdev
	cp -f $app_path/tpservice-$libusb_ver ./tpservice
	cp -f $app_path/setpdrv-$libusb_ver ./setpdrv
	cp -f $tools_path/Touchpack-$gtk_ver-$libusb_ver-$libpng_ver ./Touchpack
	cp -f $tools_path/LinearAp-$gtk_ver-$libusb_ver-$libpng_ver ./LinearAp
	cp -f $tools_path/FreeDraw-$gtk_ver-$libusb_ver-$libpng_ver ./FreeDraw
	cp -f $tools_path/Touch_Alignment-$gtk_ver-$libusb_ver-$libpng_ver ./Touch_Alignment
fi

chmod 777 ./*

old_xinput=0
if [ "$os_name" = "slackware" ]; then
	if [ "$xinput_version" = "2.0" ]; then
		old_xinput=1
	fi
fi

if [ "$xinput_version" = "0.6" ] || [ "$xinput_version" = "0.7" ]; then
	old_xinput=1
fi

if [ "$old_xinput" = "1" ]; then
	driver_name=xfhiddrv_drv.so
else
	driver_name=risintech_drv.so
fi

if [ -f $driver_path/$driver_name-$os_name ]; then
	cp -f $driver_path/$driver_name-$os_name $driver_path/$driver_name
fi

driver_file=$driver_path/$driver_name

if [ ! -f $driver_file ]; then
	echo "This X Server version is not supported. [driver 1]"
	exit 1
fi

cd ..

if [ "$uin" = "1" ]; then
	sh setup-wayland.sh $os_name $xinput_version $machine $type $os_ver $driver_file $wayland_enable
else
	sh setup.sh $os_name $xinput_version $machine $type $os_ver $driver_file $driver_path
fi
